﻿using ConfExpert.Forms.Base;
using DevTeam;


using System;
using System.Data;

namespace ConfExpert.Forms.SatinAlma.YeniYapi.Confirmation
{
    public partial class DuzeltY : XForm
    {
        public DuzeltY()
        {
            InitializeComponent();
        }

        private void DuzeltY_Load(object sender, EventArgs e)
        {
            gridView1.Init("DuzeltY", "");
            gridView1.AddDetailGrid(gridView2, "AyrimlarId");

            //gridView1.SutunEkle("KonfMt", typeof(decimal), "SUM(Child({FK}).KonfMt)");
            //gridView1.SutunEkle("YuklenenMt", typeof(decimal), "SUM(Child({FK}).YuklenenMt)");
            gridView1.AddColumn("OranY", typeof(decimal), "YuklenenMt/KonfMt");
            gridView1.AddColumn("OranS", typeof(decimal), "SipMt/KonfMt");

            gridView1.AddColumn("Adet", typeof(decimal), "COUNT(Child({FK}).KonfId)");

            gridView1.FormatAsNumber(2, "OranY", "OranS");
            gridView1.FormatAsNumber(1, "KonfMt", "YuklenenMt", "SipMt");
            gridView1.AddSummary("KonfMt", "YuklenenMt", "SipMt");

            gridView2.FormatAsNumber(1, "KonfMt", "YuklenenMt");
            gridView2.AddSummary("KonfMt", "YuklenenMt");
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();

            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            splitContainerControl1.AutoSize();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataTable d = new DataTable("Merge");
            d.Columns.Add("AyrimlarId", typeof(int));
            d.Columns.Add("KonfMt", typeof(decimal));
            d.Columns.Add("KonfId", typeof(int));
            d.Columns.Add("YuklenenMt", typeof(decimal));

            gridView1.EndEditing();
            gridView2.EndEditing();

            foreach (int rowHandle in gridView2.GetSelectedRows())
                if (rowHandle > -1)
                    if (DevExpress.XtraGrid.GridControl.NewItemRowHandle != gridView2.FocusedRowHandle &&
                        DevExpress.XtraGrid.GridControl.AutoFilterRowHandle != gridView2.FocusedRowHandle)
                        d.Rows.Add(gridView2.GetRowCellValue(rowHandle, "AyrimlarId"),
                            gridView2.GetRowCellValue(rowHandle, "KonfMt"),
                            gridView2.GetRowCellValue(rowHandle, "KonfId"),
                            gridView2.GetRowCellValue(rowHandle, "YuklenenMt")
                            );

            decimal q = d.Compute("SUM(KonfMt)", string.Empty).ToDecimal();
            decimal k = d.Compute("SUM(YuklenenMt)", string.Empty).ToDecimal();
            if (q != k)
            {
                ShowErrorMessage("Seçilen satırların YuklenenMt ve AyrılanMt toplamları eşit olmalı!");
                return;
            }

            ExecuteNonQuery("DuzeltY", d.Serialize());
            Notify(gridView1.GetFocusedDataRow()["AyrimlarId"].ToStr());
            
            /*
            gridView1.VeriyiGuncelle(true, "DuzeltY", d.Serialize());
            gridView1.BestFitColumns();
            gridView2.BestFitColumns();
             */
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gridView2.BestFitColumns();
        }
    }
}
